﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using UnitTestUtils.UI.Automation;
using AppToolkit.WPF.UI;
using System.Windows.Automation;
using System.IO;

namespace AppToolkit.WPF.Tests.UI
{
    [TestFixture]
    public class ErrorReportDialog_automated_UIAutomation
    {
        UIAutomationHost<ErrorReportDialog> _host;

        #region Setup / Teardown
        [SetUp]
        public void Setup()
        {
            Exception ex = ErrorReportDialog_automated.generateAndSetException();
            _host = UIAutomationHost<ErrorReportDialog>.Create((d) =>
            {
                d.Exception = ex;
            });
        }

        [TearDown]
        public void Dispose()
        {
            //TODO: Aufraeumen des Dispatchers funktioniert nicht!
            _host.Dispose();
            _host = null;

            UnitTestUtils.UI.UITestHelper.CleanupDispatcher();
        }
        #endregion

        [Test, RequiresSTA, Ignore]
        public void Safe_to_file()
        {
            var tmpFile = System.IO.Path.GetTempFileName() + ".txt";

            var strm = new MemoryStreamEx();

            var mock = new Moq.Mock<IStorageProvider>();
            mock.Setup(f => f.GetStoreStream(Moq.It.Is<string>(name => name == tmpFile))).Returns(strm);

            _host.NativeControl.StorageProvider = mock.Object;

            AutomationElement wnd = _host.Window;
            AutomationElement targetControl = _host.Control;

            InvokePattern saveButton = (InvokePattern)targetControl
                .FindFirst(TreeScope.Children, ConditionHelper.ByAutomationID("btnSaveToFile"))
                .GetCurrentPattern(InvokePattern.Pattern);
            saveButton.Invoke();

            _host.WaitForInputIdle();

            var wndElement = targetControl.FindFirst(TreeScope.Children, ConditionHelper.ByControlType(ControlType.Window));
            WindowPattern wndPattern = wndElement.ToPattern<WindowPattern>(WindowPattern.Pattern);
            _host.WaitForInputIdle();

            // type filename
            AutomationElement txtBox = wndElement.FindFirst(TreeScope.Descendants, ConditionHelper.ByAutomationID("FileNameControlHost"));
            ValuePattern v = (ValuePattern)txtBox.GetCurrentPattern(ValuePattern.Pattern);
            v.SetValue(tmpFile);

            _host.WaitForInputIdle();
            //MessageBox.Show("Hook Event");

            // press OK
            AutomationElement okButton = wndElement.FindFirst(TreeScope.Descendants, ConditionHelper.ByAutomationID("1")); //ConditionHelper.ByName("Speichern"));
            okButton.SetFocus();
            InvokePattern invoke = (InvokePattern)okButton.GetCurrentPattern(InvokePattern.Pattern);
            invoke.Invoke();

            _host.WaitForInputIdle();

            Assert.IsTrue(strm.IsDisposed);

            InvokePattern ignoreButton = (InvokePattern)targetControl
                .FindFirst(TreeScope.Children, ConditionHelper.ByAutomationID("btnIgnore"))
                .GetCurrentPattern(InvokePattern.Pattern);
            ignoreButton.Invoke();
        }


        class MemoryStreamEx : MemoryStream
        {
            public bool IsDisposed { get; private set; }

            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
                this.IsDisposed = true;
            }
        }
    }
}
